<?php
require ('../include/init.inc.php');
$tc_id = $tc_name = $tc_phone = $tc_sex = $tc_age = $tc_id_type = $tc_num = $tc_star = $tc_photo = '';
extract ( $_POST, EXTR_IF_EXISTS );

if (Common::isPost ()) {
    
    $pic = $_FILES['tc_photo']['tmp_name'];
    if($pic == "") {
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/travel_client_info.php");
    }
    $upload_ret = false;
    if($pic){
        // 上传的路径
        $photoDir = $_SERVER['DOCUMENT_ROOT']."/photos/travel/travelClient";
        // 创建文件夹
        if(!file_exists($photoDir)){
            mkdir($photoDir, 0777);
        }
        // 用时间戳来保存图片，防止重复
        $timeNow = time();
        $targetFile = $photoDir . '/' . $timeNow.'.jpg';
        $sqlFile = "/photos/travel/travelClient/".$timeNow.'.jpg';
        echo $targetFile."<br / >";
        // 将临时文件 移动到我们指定的路径，返回上传结果
        $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
    }
    if($upload_ret == false){
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/travel_client_info.php");
    }
    
	$exist = TravelClientInfo::getTravelClientInfoById($tc_id);
	if($exist){
		
		OSAdmin::alert("error",ErrorMessage::STOCK_CONFLICT);
	}else if($tc_id=="" || $tc_name==""){
		
		OSAdmin::alert("error",ErrorMessage::NEED_PARAM);
	}else{
		$input_data = array ('tc_id' => $tc_id, 'tc_name' => $tc_name, 'tc_phone' => $tc_phone, 'tc_sex' => $tc_sex, 'tc_age' => $tc_age
		    , 'tc_id_type' => $tc_id_type, 'tc_num' => $tc_num, 'tc_star' => $tc_star, 'tc_photo' => $sqlFile
		);
		$tc_id = TravelClientInfo::addTravelClientInfo ( $input_data );
		echo $tc_id;
		if ($tc_id ==0) {
			
			SysLog::addLog ( UserSession::getUserName(), 'ADD', 'tc_id' ,$tc_id, json_encode($input_data) );
			Common::exitWithSuccess ('账号添加成功','panel/travel_client_info.php');
		}else{
			OSAdmin::alert("error");
		}
	}
}
// 获取旅游客户id
$tc_id_post = TravelClientInfo::getTravelClientMaxId();
Template::assign ( 'type_list', $baseConf["optionList"] );
Template::assign("_POST" ,$_POST);
Template::assign("tc_id_posts" ,$tc_id_post);
Template::display ( 'panel/travel_client_info_add.tpl' );
